package by.apiskun.curex.services.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.apiskun.curex.dataaccess.ChangeRateDAO;
import by.apiskun.curex.datamodel.ChangeRate;
import by.apiskun.curex.services.ChangeRateService;

@Service
public class ChangeRateServiceImpl implements ChangeRateService {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(ChangeRateServiceImpl.class);
	@Inject
	private ChangeRateDAO dao;

	@PostConstruct
	private void init() {
		// this method will be called by Spring after bean instantiation. Can be
		// used for any initialization process.
		LOGGER.info("Instance of ChangeRateService is created. Class is: {}",
				getClass().getName());
	}

	@Override
	public ChangeRate get(Long id) {
		ChangeRate entity = dao.getById(id);
		return entity;
	}

	@Override
	public void saveOrUpdate(ChangeRate changeRate) {
		if (changeRate.getId() == null) {
			LOGGER.debug("Save new: {}", changeRate);
			dao.insert(changeRate);
		} else {
			LOGGER.debug("Update: {}", changeRate);
			dao.update(changeRate);
		}
	}

	@Override
	public Long getCount() {
		Long count = dao.getCount();
		return count;
	}

	@Override
	public List<ChangeRate> getAllChangeRates() {
		List<ChangeRate> entity = dao.getAllChangeRates();
		return entity;
	}

	@Override
	public void updateChangeRateCourse(Long id, Double newCourse) {
		dao.updateChangeRateCourse(id, newCourse);

	}

	@Override
	public void updateChangeRateEndDate(Long id, Date newEndDate) {
		dao.updateChangeRateEndDate(id, newEndDate);

	}

	@Override
	public List<ChangeRate> getAllChangeRatesByBeforeName(String beforeName) {
		List<ChangeRate> entity = dao.getAllChangeRatesByBeforeName(beforeName);
		return entity;
	}

	@Override
	public List<ChangeRate> getAllChangeRatesByAfterName(String afterName) {
		List<ChangeRate> entity = dao.getAllChangeRatesByAfterName(afterName);
		return entity;
	}

	@Override
	public List<ChangeRate> getAllChangeRatesForDay(Date date) {
		List<ChangeRate> entity = dao.getAllChangeRatesForDay(date);
		return entity;
	}

	@Override
	public void delete(ChangeRate changeRate) {
		LOGGER.debug("Remove: {}", changeRate);
		dao.delete(changeRate.getId());

	}

	@Override
	public void deleteAll() {
		LOGGER.debug("Delete all changeRate");
		dao.deleteAll();

	}

}
